package com.mall.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.mall.product.domain.res.PmsProductCategoryRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.product.mapper.PmsProductCategoryMapper;
import com.mall.product.domain.PmsProductCategory;
import com.mall.product.service.ICategoryService;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-18
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private PmsProductCategoryMapper categoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public PmsProductCategory selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param category 商品分类
     * @return 商品分类
     */
    @Override
    public List<PmsProductCategory> selectCategoryList(PmsProductCategory category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    public int insertCategory(PmsProductCategory category)
    {
        if(category.getParentId()==0){
            category.setParentId(Long.valueOf(0));
            category.setLevel(1);
        }else {
            category.setLevel(2);
        }
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品分类
     *
     * @param category 商品分类
     * @return 结果
     */
    @Override
    public int updateCategory(PmsProductCategory category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<PmsProductCategoryRes> queryProductLevel1ClassificationInformation() {
        // 查询所有第一级分类
        List<PmsProductCategory> topLevelCategories = categoryMapper.selectCategoryList(new PmsProductCategory());

        // 转换为PmsProductCategoryRes列表
        List<PmsProductCategoryRes> topLevelCategoriesRes = new ArrayList<>();
        for (PmsProductCategory category : topLevelCategories) {
            PmsProductCategoryRes categoryRes = convertToRes(category);
            topLevelCategoriesRes.add(categoryRes);
        }

        return topLevelCategoriesRes;
    }



    private PmsProductCategoryRes convertToRes(PmsProductCategory category) {
        PmsProductCategoryRes categoryRes = new PmsProductCategoryRes();
        categoryRes.setId(category.getId());
        categoryRes.setName(category.getName());

        // 递归查询子分类
        List<PmsProductCategory> subcategories = categoryMapper.selectSubcategories(category.getId());
        List<PmsProductCategoryRes> childList = new ArrayList<>();
        for (PmsProductCategory subcategory : subcategories) {
            PmsProductCategoryRes childCategoryRes = convertToRes(subcategory);
            childList.add(childCategoryRes);
        }
        categoryRes.setChildList(childList);

        return categoryRes;
    }
    @Override
    public List<PmsProductCategory> selectDropDown() {
        return categoryMapper.selectDropDown();
    }

}
